package top.infopub.util;


import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * html元素替换类
 * @author Awoke
 * @version 2018-09-14
 * @see HtmlUtil
 * @since
 */
public class HtmlUtil {

    // 定义HTML标签的正则表达式  
    private static final String regEx_html = "<[^>]+>";

    // 定义空格回车换行符  
    private static final String regEx_space = "\\s*|\t|\r|\n";

    /**
     * 删除html标签
     * @param htmlStr
     * @return 
     * @see
     */
    public static String getTextFromHtml(String htmlStr) {
        htmlStr = delHTMLTag(htmlStr);
        htmlStr = htmlStr.replaceAll("&nbsp;", "");
        return htmlStr;
    }

    /** 
     * @param htmlStr 
     * @return  删除Html标签 
     * 
     */
    private static String delHTMLTag(String htmlStr) {
        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); // 过滤html标签  

        Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
        Matcher m_space = p_space.matcher(htmlStr);
        htmlStr = m_space.replaceAll(""); // 过滤空格回车标签  
        return htmlStr.trim(); // 返回文本字符串  
    }

    /**
     * test
     * @param args 
     * @see
     */
    public static void main(String[] args) {
        String str = "<p><div style='text-align:center;'> 整治“四风” &nbsp;  清弊除垢<br/><span style='font-size:14px;'> </span><span style='font-size:18px;'>，公司召开党的群众路线教育实践活动动员大会</span><br/></div></p>";
        System.out.println(getTextFromHtml(str));
    }

}
